package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.res.*;
import com.easy.dfcapi.service.ApiProductService;
import com.easy.dfcapi.service.ApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 商品及购物车相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/product")
public class ApiProductController extends Api20Controller {


    @Autowired
    private ApiProductService productService;


    /**
     * 品牌增加
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BrandAdd", method = RequestMethod.POST)
    public Result BrandAdd(Response<BrandRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.BrandAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 品牌修改
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BrandMod", method = RequestMethod.POST)
    public Result BrandMod(Response<BrandRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.BrandMod(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 品牌列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BrandList", method = RequestMethod.POST)
    public Result BrandList(Response<BrandRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.BrandList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 品牌删除
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "BrandDel", method = RequestMethod.POST)
    public Result BrandDel(Response<BrandRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.BrandDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 类别增加
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CategoryAdd", method = RequestMethod.POST)
    public Result CategoryAdd(Response<CategoryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.CategoryAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 类别修改
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CategoryMod", method = RequestMethod.POST)
    public Result CategoryMod(Response<CategoryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.CategoryMod(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 类别列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CategoryList", method = RequestMethod.POST)
    public Result CategoryList(Response<CategoryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.CategoryList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 类别删除
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "CategoryDel", method = RequestMethod.POST)
    public Result CategoryDel(Response<CategoryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.CategoryDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
    /**
     * 发布商品属性页
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ProductPreAdd", method = RequestMethod.POST)
    public Result ProductPreAdd(Response<ProductRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.ProductPreAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商品增加
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ProductAdd", method = RequestMethod.POST)
    public Result ProductAdd(Response<ProductRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.ProductAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商品修改
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ProductMod", method = RequestMethod.POST)
    public Result ProductMod(Response<ProductRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.ProductMod(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商品列表
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ProductList", method = RequestMethod.POST)
    public Result ProductList(Response<ProductRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.ProductList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商品删除
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ProductDel", method = RequestMethod.POST)
    public Result ProductDel(Response<ProductRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.ProductDel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }

    /**
     * 商品上架&下架处理
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ProductSales", method = RequestMethod.POST)
    public Result ProductSales(Response<ProductRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return productService.ProductSales(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            return rt;
        }
    }
}
